package com.qa.demo.maindriver;

import com.qa.demo.utils.Configuation;

import java.io.*;
import java.util.*;

/**
 * Created by Devin Hua on 2017/8/23.
 * MAIN DRIVER FUNCTION:
 * to get users' questions and return relevant answers;
 */

public class MainDriver {

    public static String inputQuestion(){

        String input_string = "";
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            input_string = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return input_string;
    }

    public static ArrayList<HashMap<String,String>> processQuestionByES(String input_string){

        //TODO 根据ES处理问题，返回的为map的list，表示返回了多个候选问题-答案对；
        //map中的键为：问题(QUESTION)、答案(ANSWER)、类型(CATEGORY)、ES评分(SCORE)，值均为string类型；
        //每一个被ES检索到的问题-答案对，用一个map表示，最多返回10个候选问题-答案对；
        ArrayList<HashMap<String,String>> list = null;
        String input_question = input_string;
        return list;
    }

    public static String getAnswers(ArrayList<HashMap<String,String>> list){

        int count_number = 0;
        String output = "";
        for(HashMap<String,String> temp_map:list)
        {
            output+=count_number++;
            output+=":\r\n";
            output+="Question: "+temp_map.get(Configuation.QUESTION)+"\r\n";
            output+="Answer: "+temp_map.get(Configuation.ANSWER)+"\r\n";
            output+="ES Score: "+temp_map.get(Configuation.ESSCORE)+"\r\n";
        }
        return output;
    }

    public static void IOTest(){

        String input_question = "";
        String output_question = "";
        ArrayList<HashMap<String,String>> list = null;
        System.out.println("Enter a string:");
        input_question = inputQuestion();
        while((!input_question.equals("EXIT!"))
                &&(!input_question.equals(""))&&input_question!=null)
        {
            list = processQuestionByES(input_question);
            output_question = getAnswers(list);
            System.out.print("After ES processing, returned answers are:\r\n");
            System.out.println(output_question);
            System.out.println("Enter a string:");
            input_question = inputQuestion();
        }
    }


}
